using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace Watchfire.DefectLogger
{
	/// <summary>
	/// Access to the registry for AppScan Extensions
	/// </summary>
	class ExtensionRegistry
	{
		/// <summary>
		/// Put all extension registry stuff under the Extensions subkey
		/// </summary>
		private static readonly string _regExtensions = "Extensions";
		

		/// <summary>
		/// Read a string value from the registry
		/// </summary>
		/// <param name="appScanRegistryPath">Path to AppScan registry</param>
		/// <param name="subkey">Subkey of the Extensions subkey</param>
		/// <param name="stringValue">Name of the string value to be retrieved</param>
		/// <returns>Value of the string value retrieved or null if it couldn't be found</returns>
		public static string Read(string appScanRegistryPath, string subkey, string stringValue)
		{
			// If the read fails, just don't load the settings
			string result = null;
			try
			{
				RegistryKey AppScanKey = Registry.CurrentUser.OpenSubKey(appScanRegistryPath);
				RegistryKey ExtensionsKey = AppScanKey.OpenSubKey(_regExtensions);
				if (ExtensionsKey != null)
				{
					RegistryKey DefectLoggerExtensionKey = ExtensionsKey.OpenSubKey(subkey);
					if (DefectLoggerExtensionKey != null)
					{
						result = DefectLoggerExtensionKey.GetValue(stringValue).ToString();
						DefectLoggerExtensionKey.Close();
					}
					ExtensionsKey.Close();
				}
				AppScanKey.Close();
			}
			catch (Exception ex)
			{
				// TODO
				Console.Out.Write(ex.ToString());
			}
			return result;
		}

		/// <summary>
		/// Write a string to the registry
		/// </summary>
		/// <param name="appScanRegistryPath">Path to AppScan registry</param>
		/// <param name="subkey">Subkey of the Extensions subkey</param>
		/// <param name="stringValue">Name of the string value to be written</param>
		/// <param name="stringValueValue">Value fo the string value to be written</param>
		public static void WriteSettings(string appScanRegistryPath, string subkey, string stringValue, string stringValueValue)
		{
			// Write shouldn't fail, but a extension shouldn't mess up the whole product
			try
			{
				RegistryKey AppScanKey = Registry.CurrentUser.OpenSubKey(appScanRegistryPath, true);
				RegistryKey ExtensionsKey = AppScanKey.OpenSubKey(_regExtensions, true);
				if (ExtensionsKey == null)
					ExtensionsKey = AppScanKey.CreateSubKey(_regExtensions, RegistryKeyPermissionCheck.ReadWriteSubTree);

				RegistryKey DefectLoggerExtensionKey = ExtensionsKey.OpenSubKey(subkey, true);
				if (DefectLoggerExtensionKey == null)
					DefectLoggerExtensionKey = ExtensionsKey.CreateSubKey(subkey, RegistryKeyPermissionCheck.ReadWriteSubTree, AppScanKey.GetAccessControl());

				DefectLoggerExtensionKey.SetValue(stringValue, stringValueValue);

				DefectLoggerExtensionKey.Close();
				ExtensionsKey.Close();
				AppScanKey.Close();
			}
			catch (Exception ex)
			{
				// TODO
				Console.Out.Write(ex.ToString());
			}
		}
	}
}
